package com.tuling.lambda;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Predicate;

/**
 * 使用java内置的四大核心函数式接口举两个例子
 * 我觉得这句话说得好：想要使用Lambda表达式，那么就一定需要函数式接口，Lambda就是函数式接口的实例。
 *      Lambda就是函数式接口的实例！
 *      Lambda就是函数式接口的实例！
 *      Lambda就是函数式接口的实例！
 *
 * @author: chenhs
 * @date: Created in 17:06 2022/7/13
 **/
public class LambdaTest2 {

    public void happyTime(double money, Consumer<Double> consumer) {
        consumer.accept(money);
    }


    @Test
    public void test(){
        //普通写法
        happyTime(500, new Consumer<Double>() {
            @Override
            public void accept(Double aDouble) {
                System.out.println("学习好累啊，出去嗨一下，花了" + aDouble + "元");
            }
        });
        //lambda
        happyTime(400, aDouble -> System.out.println("学习好累啊，出去嗨一下，花了" + aDouble + "元"));
    }


    public List<String> filterString(List<String> list, Predicate<String> predicate){
        List<String> filterList = new ArrayList<>();
        for(String s : list) {
            if (predicate.test(s)){
                filterList.add(s);
            }
        }
        return filterList;
    }

    @Test
    public void test2(){
        List<String> list = Arrays.asList("北京", "南京", "天津", "东京", "西京", "普京");
        //普通写法
        List<String> filterList = filterString(list, new Predicate<String>() {
            @Override
            public boolean test(String s) {
                return s.contains("京");
            }
        });
        System.out.println(filterList);
        //lambda
        List<String> filterList2 = filterString(list, s -> s.contains("京"));
        System.out.println(filterList2);
    }

}
